﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;


public class AvatarAction
{
    public AvatarHandle Handle;
    public bool CanMultipleAction = true;
    private const float Delta = 0.2f;
    private Transform transform;
    public Dictionary<UInt16, ActionParameter> ActionList;
    public AvatarAction(AvatarHandle handle)
    {
        Handle = handle;
        transform = Handle.Root.transform;
        ActionList = ActionConfig.GetActionList(Handle.Job);
    }
    public void Update()
    {
        if (Handle.IsOwner)
        {
            if (InputManager.OwnerInput.Fire1 && !Handle.Movement.InitiativeJump && !InputManager.OwnerInput.Run)
            {
                if (Handle.Animation.CurrentAction != null)
                {
                    switch (Handle.Animation.CurrentAction.Parameter.Index)
                    {
                        case 1:
                            {
                                DoAction(2, true);
                                break;
                            }
                        case 2:
                            {
                                DoAction(3, true);
                                break;
                            }
                    }
                }
                else
                {
                    DoAction(1, true);
                }
            }
            if (Handle.InputHandle.ActionIdle)
            {
                if (InputManager.OwnerInput.Fire1)
                {
                    if (Handle.Movement.InitiativeJump) DoAction(4, true);
                    else 
                    {
                        if (InputManager.OwnerInput.Run) DoAction(6, true);
                    }
                }
                if (InputManager.OwnerInput.Action1)
                {
                    DoAction(5, true);
                }
                if (InputManager.OwnerInput.Action2)
                {
                    //DoAction(6);
                }
            }
            if (InputManager.OwnerInput.Suck) AppInterface.ActionModule.SuckSoul(Handle.Weapon.transform.position);
            InputManager.OwnerInput.Fire1 = false;
            InputManager.OwnerInput.Fire2 = false;
            InputManager.OwnerInput.Fire3 = false;
            InputManager.OwnerInput.Action1 = false;
            InputManager.OwnerInput.Action2 = false;
        }
    }
    public void DoAction(UInt16 actionId, bool net)
    {
        if (ActionList.ContainsKey(actionId) && !Handle.Animation.ExsitAction(actionId))
        {
            ActionBase item = ActionConfig.GetAction(ActionList[actionId]);
            //if (item.Parameter.Grounded == InputManager.OwnerInput.Grounded)
            //{
                Handle.InputHandle.ActionIdle = false;
                item.Handle = Handle;
                Handle.Animation.ActionAnimation(item);
                if (net) Handle.Module.BroadcastAction(actionId);
            //}
        }
    }
    public void OnBeat(ActionBase ation, int hp, bool net)
    {
        Handle.Animation.OnBeat();
        
        AppInterface.GUIModule.ShowScore(Handle.NameTagTransform, hp);
        if (net) Handle.Module.BroadcastOnBeat(Handle.Id, ation.Id);
    }
    public static Dictionary<string, Vector3> SpeedList = new Dictionary<string, Vector3>();
}

